/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FramePrueba.java
 *
 * Created on 20/01/2010, 12:47:45 PM
 */

package prueba;

import com.jturingmachinele.graphics.estados.EstadoFinal;
import com.jturingmachinele.graphics.estados.EstadoInicial;
import com.jturingmachinele.graphics.estados.EstadoTransitivo;
import com.jturingmachinele.graphics.transiciones.TransicionCiclo;
import com.jturingmachinele.graphics.transiciones.TransicionRecta;
import com.jturingmachinele.visual.componentes.Lienzo;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

/**
 *
 * @author jsandoval
 */
public class FramePrueba extends javax.swing.JFrame {
EstadoInicial ei = null;
boolean band = false;
EstadoTransitivo et = null;
EstadoFinal fin = null;
TransicionRecta tran = null;
TransicionRecta tran2 = null;
TransicionCiclo tranC = null;
Lienzo lienzo = new Lienzo();
private JScrollPane scroll;
    /** Creates new form FramePrueba */
    public FramePrueba() {
        

        initComponents();
        
        scroll=new JScrollPane(lienzo,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        getContentPane().add(scroll,java.awt.BorderLayout.CENTER);
        Graphics g=lienzo.getGraphics();
        //ei = EstadoInicial.getInstancia(new Point(100,250), "q1");
        //lienzo.getObjetosGraficos().add(ei);
        et = new EstadoTransitivo(new Point(250,350), "q2");
        //Estado estado = ei;
        fin = new EstadoFinal(new Point(250,100), "q4");
        tran = new TransicionRecta(ei, et, "bDe");
        tran2 = new TransicionRecta(ei, fin, "aDc");
//
        lienzo.setObjetoGrafico(ei);
        lienzo.setObjetoGrafico(et);
        lienzo.setObjetoGrafico(fin);
        lienzo.setObjetoGrafico(tran);
        lienzo.setObjetoGrafico(tran2);
        lienzo.setObjetoGrafico(tranC);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setExtendedState(MAXIMIZED_BOTH);
        setMinimumSize(new java.awt.Dimension(800, 600));
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        jToggleButton1.setText("Exitar");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jToggleButton1);

        jButton3.setText("<");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);

        jButton4.setText(">");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4);

        jButton5.setText("Eliminar TRansicion");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton5);

        jButton6.setText("Eliminar Estado");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton6);

        jButton7.setText("Nuevo estado");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton7);

        jButton8.setText("Nueva transicion");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton8);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        if(jToggleButton1.isSelected()){
            ei.exitar();
            et.exitar();
            fin.exitar();
            //this.repaint();
        }else{
            ei.desexitar();
            et.desexitar();
            fin.desexitar();
            //this.repaint();
        }
    }//GEN-LAST:event_jToggleButton1ActionPerformed
boolean banderaMov = false;
boolean banderaDOS = true;
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        // TODO add your handling code here:
        //System.out.println(String.format("[%1$d,%2$d]",evt.getX(), evt.getY()));
        //System.out.println(String.format("[%1$d,%2$d]",ei.getCoordenadaXY().x, ei.getCoordenadaXY().y));
        bandera = false;//Bandera de arrastrar
        
        if(ei.isFlotando(evt.getX(), evt.getY())){
            banderaMov = true;
        }else{
            banderaMov = false;
        }
        if(banderaMov){
            if(banderaDOS){
                ei.exitar();
                et.exitar();
                fin.exitar();
                //this.repaint();
            }
            banderaDOS = false;
        }
        if(!banderaMov && !banderaDOS){
            ei.desexitar();
            et.desexitar();
            fin.desexitar();
            //this.repaint();
            banderaDOS = true;
        }
//        if(ei.isFlotando(evt.getX(), evt.getY())){
//           // band = false;;
//            ei.exitar();
//            this.repaint();
//        }else{
//            band = false;
//            ei.desexitar();
//            this.repaint();
//        }
//        if(band){
//            this.repaint();
//            band = false;
//        }
    }//GEN-LAST:event_formMouseMoved
boolean bandera = false;
    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        // TODO add your handling code here:

        if(ei.isFlotando(evt.getX(), evt.getY()) && bandera == false){
            bandera = true;
        }
        if(bandera){
            ei.setCoordenadaXY(new Point(evt.getX() - EstadoInicial.RADIO, evt.getY() - EstadoInicial.RADIO));
            //this.repaint();
        }

    }//GEN-LAST:event_formMouseDragged

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if(!lienzo.isMementoMinimo()){
            lienzo.deshacer();
            jButton4.setEnabled(true);
        }else{
            jButton3.setEnabled(false);
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if(!lienzo.isMementoMaximo()){
            lienzo.rehacer();
            jButton3.setEnabled(true);
        }else{
            jButton4.setEnabled(false);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        lienzo.eliminarTransicion(JOptionPane.showInputDialog(this,"Etiqeuta"));
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        lienzo.eliminarEstado(JOptionPane.showInputDialog(this,"Etiqeuta"));
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        lienzo.setObjetoGrafico(new EstadoTransitivo(new Point(Integer.parseInt(JOptionPane.showInputDialog(this,"x")),Integer.parseInt(JOptionPane.showInputDialog(this,"y"))),JOptionPane.showInputDialog(this,"Etiqueta")));
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        //lienzo.setObjetoGrafico(new TransicionCiclo(fin,fin,"aVo"));
    }//GEN-LAST:event_jButton8ActionPerformed


//    @Override
//    public void paint(Graphics g){
//        super.paint(g);
//        Graphics2D g2d=(Graphics2D)g;
//        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
//                             RenderingHints.VALUE_ANTIALIAS_ON);
//        //g2d.scale(2, 2);
//        g2d.setStroke (new BasicStroke(1.2f));
//        ei.dibujar(g);
//        et.dibujar(g);
//        fin.dibujar(g);
//        tran.dibujar(g);
//        tran2.dibujar(g);
//        tranC.dibujar(g);
//    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FramePrueba().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables

}
